package com.monlyu.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.model.CountFGModel;
import com.monlyu.model.LevelScoreQueryModel;
import com.monlyu.model.QueryModel;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.vo.GaoJian2Reporter;
import com.monlyu.vo.ManuScAudit;
import com.monlyu.vo.Manuscript;

/**
 * @author SongJun
 * @since 2010-3-1
 * @description 各类统计bo
 */
@Repository(value = "statisticsBo")
public class StatisticsBo extends BaseBo {

	/**
	 * @author SongJun
	 * @since 2010-3-14
	 * @description 稿件查询
	 */
	public List<GaoJian2Reporter> getQueryGJRS(QueryModel qm) {
		StringBuilder sb = new StringBuilder();
		sb = sb.append(" from com.monlyu.vo.GaoJian2Reporter m where 1=?");
		List<Object> args = new ArrayList<Object>();
		args.add(1);
		if (null != qm) {
			if (null != qm.getBegDate()) {
				sb.append(" and m.manuscript.createDate >=?");
				args.add(qm.getBegDate());
			}
			if (null != qm.getEndDate()) {
				sb.append(" and m.manuscript.createDate <=?");
				args.add(qm.getEndDate());
			}
			if (null != qm.getUserId()) {
				sb.append(" and m.reporter.id =?");
				args.add(qm.getUserId());
			}
			if (null != qm.getChannelType()) {
				sb.append(" and m.manuscript.bcType =?");
				args.add(qm.getChannelType());
			}
			if (null != qm.getProgramType()) {
				sb.append(" and m.manuscript.column =?");
				args.add(qm.getProgramType());
			}
			if (!StringUtil.isEmpty(qm.getTitle())) {
				if (qm.isExact()) {
					sb.append(" and m.manuscript.title =?");
					args.add(qm.getTitle().trim());
				} else {
					sb.append(" and m.manuscript.title like ?");
					args.add("%" + qm.getTitle().trim() + "%");
				}
			}
			if (null != qm.getRegionType()) {
				sb.append(" and m.manuscript.where =?");
				args.add(qm.getRegionType());
			}
			if (null != qm.getManuscriptType()) {
				sb.append(" and m.manuscript.category =?");
				args.add(qm.getManuscriptType());
			}
			if (null != qm.getSubmatterTypet()) {
				sb.append(" and m.manuscript.owetype =?");
				args.add(qm.getSubmatterTypet());
			}
			if (-1 != qm.getAuditZt()) {
				sb.append(" and m.manuscript.auditZt =?");
				args.add(qm.getAuditZt());
			}
			if (0 != qm.getTimes()) {
				sb.append(" and m.manuscript.times =?");
				args.add(qm.getTimes());
			}
			if (null != qm.getUnit()) {
				sb.append(" and m.manuscript.unit =?");
				args.add(qm.getUnit());
			}
		}
		return dao.find(sb.toString(), args.toArray());
	}

	/**
	 * 记者发稿统计，按另外一种方式统计
	 * 
	 * @param qm
	 * @return
	 * @since chendan 2010-6-1
	 */
	public List<Manuscript> getQueryGJRS1(QueryModel qm) {
		StringBuilder sb = new StringBuilder();
		sb = sb.append(" from com.monlyu.vo.Manuscript m where m.adv=false  ");
		List<Object> args = new ArrayList<Object>();
		if (null != qm) {
			if (null != qm.getBegDate()) {
				sb.append(" and m.createDate >=?");
				args.add(qm.getBegDate());
			}
			if (null != qm.getEndDate()) {
				sb.append(" and m.createDate <=?");
				args.add(DateUtil.addDay(qm.getEndDate(), 1));
			}
			if (qm.getUbeginDate() != null) {
				sb.append(" and m.usedDate >=?");
				args.add(qm.getUbeginDate());
			}
			if (qm.getUendDate() != null) {
				sb.append(" and m.usedDate <=?");
				args.add(DateUtil.addDay(qm.getUendDate(), 1));
			}
			if (null != qm.getChannelType()) {
				sb.append(" and m.bcType =?");
				args.add(qm.getChannelType());
			}
			if (null != qm.getProgramType()) {
				sb.append(" and m.column =?");
				args.add(qm.getProgramType());
			}
			if (!StringUtil.isEmpty(qm.getTitle())) {
				if (qm.isExact()) {
					sb.append(" and (m.title =? or m.content=?)");
					args.add(qm.getTitle().trim());
					args.add(qm.getTitle().trim());
				} else {
					sb.append(" and (m.title like ? or m.content like ?)");
					args.add("%" + qm.getTitle().trim() + "%");
					args.add("%" + qm.getTitle().trim() + "%");
				}
			}
			if (null != qm.getRegionType()) {
				sb.append(" and m.where =?");
				args.add(qm.getRegionType());
			}
			if (null != qm.getManuscriptType()) {
				sb.append(" and m.category =?");
				args.add(qm.getManuscriptType());
			}
			if (null != qm.getSubmatterTypet()) {
				sb.append(" and m.owetype =?");
				args.add(qm.getSubmatterTypet());
			}
			if (-1 != qm.getAuditZt()) {
				sb.append(" and m.auditZt =?");
				args.add(qm.getAuditZt());
			}
			if (0 != qm.getTimes()) {
				sb.append(" and m.times =?");
				args.add(qm.getTimes());
			}
			if (null != qm.getUnit()) {
				sb.append(" and m.unit =?");
				args.add(qm.getUnit());
			}
			// 查撰稿人
			if (null != qm.getUserId()) {
				sb
						.append("and exists(select g.id from com.monlyu.vo.GaoJian2Reporter g where"
								+ " g.manuscript.id=m.id and g.reporter.id =?)");
				args.add(qm.getUserId());
			}
		}
		return dao.find(sb.toString(), args.toArray());
	}

	/**
	 * 查询稿件所有的撰稿人
	 * 
	 * @param manuscript
	 * @return
	 * @since chendan 2010-6-1
	 */
	public String getDbLhbdjzName(Manuscript manuscript) {
		String str = "";
		String hql = "select reporter.name from com.monlyu.vo.GaoJian2Reporter where "
				+ " manuscript.id="
				+ manuscript.getId()
				+ " order by reporter.name";
		List<String> list = dao.find(hql);
		for (String s : list) {
			str += s.toString();
			str += ",";

		}
		if (str.equals("")) {
			return "";
		} else {
			return (String) StringUtil.shot(str, str.length() - 1);
		}
	}

	/**
	 * 组装成对象，便于页面显示，将每个稿件的所有记录显示到一条记录中
	 * 
	 * @param qm
	 * @return
	 * @since chendan 2010-6-1
	 */
	public List<CountFGModel> countFG(QueryModel qm) {
		List<Manuscript> results = getQueryGJRS1(qm);
		List<CountFGModel> countList = Lists.newArrayList();
		for (Manuscript manuscript : results) {
			CountFGModel countFGModel = new CountFGModel();
			countFGModel.setManuscript(manuscript);
			countFGModel.setUserName(getDbLhbdjzName(manuscript));
			// 设置GaoJian2Reporter，便于页面显示记者分数情况
			String hqlString = "from com.monlyu.vo.GaoJian2Reporter g where g.manuscript=? and g.reporter=?";
			Object gjReporter = dao.findOne(hqlString, manuscript, manuscript
					.getUser());
			if (gjReporter != null) {
				countFGModel.setGJian2Reporter((GaoJian2Reporter) gjReporter);
			}
			countList.add(countFGModel);
		}
		return countList;

	}

	// 记者文稿打分相关明细统计
	public Pages<GaoJian2Reporter> getGJRStatistics(Page page, Long id) {
		Criteria criteria = dao.initCriteria(GaoJian2Reporter.class);
		criteria.createAlias("Manuscript", "manuscript");
		criteria.add(Restrictions.eq("manuscript.id", id));
		criteria.addOrder(Order.asc("id"));
		return findByCriteria(page.getNo(), page.getSize(), criteria);
	}

	/**
	 * @author SongJun
	 * @since 2010-3-15
	 * @description 返回封装好的一个打分打等的统计
	 */
	public List<LevelScoreQueryModel> getLevelScoreQuerys(
			LevelScoreQueryModel lsqm) {
		List<LevelScoreQueryModel> lsqms = new ArrayList<LevelScoreQueryModel>();
		List<Manuscript> msList = getManuscripts(lsqm);
		if (msList != null) {
			for (Manuscript ms : msList) {
				LevelScoreQueryModel lsqmtemp = new LevelScoreQueryModel();
				lsqmtemp.setManuscript(ms);
				lsqmtemp.setId(ms.getId());
				// System.out.println(ms.getId());
				ManuScAudit msa = getManuscriptById(ms.getId());
				if (null != msa) {
					lsqmtemp.setEditUser(msa.getAuditor() == null ? "" : msa
							.getAuditor().getName());
					lsqmtemp.setLevel1Score((msa.getAuditScore() == null) ? 0
							: msa.getAuditScore());
				}
				List<GaoJian2Reporter> gjrList = getGaoJian2Reporters(ms
						.getId());
				lsqmtemp.setGaoJian2Reporters(gjrList);
				lsqms.add(lsqmtemp);
			}
		}
		return lsqms;
	}

	/**
	 * @author SongJun
	 * @since 2010-3-15
	 * @description 返回每份文稿的相关人士
	 */
	public List<GaoJian2Reporter> getGaoJian2Reporters(Long manuscriptid) {
		StringBuilder sb = new StringBuilder();
		sb
				.append(" from com.monlyu.vo.GaoJian2Reporter g where g.manuscript.id =?");
		return dao.find(sb.toString(), manuscriptid);
	}

	/**
	 * @author SongJun
	 * @since 2010-3-15
	 * @description 根据id返回文稿一审的分数
	 */
	public ManuScAudit getManuscriptById(Long manuscriptid) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from com.monlyu.vo.ManuScAudit m where m.level =?");
		sb.append(" and m.manuscript.id =?");
		return (ManuScAudit) dao.findOne(sb.toString(), 1, manuscriptid);

	}

	/**
	 * @author SongJun
	 * @since 2010-3-15
	 * @description 根据条件返回要统计的文稿
	 */
	public List<Manuscript> getManuscripts(LevelScoreQueryModel lsqm) {
		StringBuilder sb = new StringBuilder();
		List<Manuscript> msList = new ArrayList<Manuscript>();
		List<Object> args = new ArrayList<Object>();
		if (null != lsqm.getUserId()) {
			sb.append(" from com.monlyu.vo.GaoJian2Reporter m where 1=1 ");
			if (lsqm.getUserType() == 1) {
				// 记者
				sb.append(" and m.reporter.id =?");
				args.add(lsqm.getUserId());
			} else if (lsqm.getUserType() == 2) {
				// 配音
				sb.append(" and m.manuscript.puser.id =?");
				args.add(lsqm.getUserId());
			} else if (lsqm.getUserType() == 3) {
				// 编辑
				sb
						.append(" and exists(select ma.id from com.monlyu.vo.ManuScAudit ma"
								+ " where ma.level =1 and ma.manuscript.id =m.manuscript.id and ma.auditor.id=?)");
				args.add(lsqm.getUserId());
			} else {
				//return null;
			}
			if (!StringUtil.isEmpty(lsqm.getTitle())) {
				sb
						.append(" and (m.manuscript.title like ? or m.manuscript.content like ?)");
				args.add("%" + lsqm.getTitle() + "%");
				args.add("%" + lsqm.getTitle() + "%");
			}
			if (-1 != lsqm.getAuditType()) {
				sb.append(" and m.manuscript.auditZt =?");
				args.add(lsqm.getAuditType());
			}
			if (null != lsqm.getBegDate()) {
				sb.append(" and m.manuscript.createDate >=?");
				args.add(lsqm.getBegDate());
			}
			if (null != lsqm.getEndDate()) {
				sb.append(" and m.manuscript.createDate <=?");
				args.add(DateUtil.addDay(lsqm.getEndDate(), 1));
			}
			if (lsqm.getUbegDate() != null) {
				sb.append(" and m.manuscript.usedDate >=?");
				args.add(lsqm.getUbegDate());
			}
			if (lsqm.getUendDate() != null) {
				sb.append(" and m.manuscript.usedDate <=?");
				args.add(DateUtil.addDay(lsqm.getUendDate(), 1));
			}
			List<GaoJian2Reporter> gjrs = dao.find(sb.toString(), args
					.toArray());
			if (gjrs.size() > 0) {
				for (GaoJian2Reporter gjr : gjrs) {
					msList.add(gjr.getManuscript());
				}
			}
		} else {
			sb.append(" from com.monlyu.vo.Manuscript m where 1=1");
			if (!StringUtil.isEmpty(lsqm.getTitle())) {
				sb.append(" and (m.title like ? or m.content like ?)");
				args.add("%" + lsqm.getTitle() + "%");
				args.add("%" + lsqm.getTitle() + "%");
			}
			if (null != lsqm.getBegDate()) {
				sb.append(" and m.createDate >=?");
				args.add(lsqm.getBegDate());
			}
			if (null != lsqm.getEndDate()) {
				sb.append(" and m.createDate <=?");
				args.add(DateUtil.addDay(lsqm.getEndDate(), 1));
			}
			if (lsqm.getUbegDate() != null) {
				sb.append(" and m.usedDate >=?");
				args.add(lsqm.getUbegDate());
			}
			if (lsqm.getUendDate() != null) {
				sb.append(" and m.usedDate <=?");
				args.add(DateUtil.addDay(lsqm.getUendDate(), 1));
			}
			if (-1 != lsqm.getAuditType()) {
				sb.append(" and m.auditZt =?");
				args.add(lsqm.getAuditType());
			}
			msList = dao.find(sb.toString(), args.toArray());
		}
		return msList;
	}

	public List<GaoJian2Reporter> getGJRList(Long id) {
		StringBuilder sb = new StringBuilder();
		sb = sb.append(" from com.monlyu.vo.GaoJian2Reporter m where m.id=?");
		return dao.find(sb.toString(), id);
	}

	/**
	 * @author SongJun
	 * @since 2010-3-4
	 * @description 统一的统计借口入口
	 * 
	 * public Pages<GaoJian2Reporter> getOBJStatistics(Page page,QueryModel
	 * qm,String order){ Criteria criteria =
	 * dao.initCriteria(GaoJian2Reporter.class); //criteria.createAlias("user",
	 * "user"); if(null != qm){ criteria = getQueryCriteria(criteria, qm); }
	 * criteria.addOrder(Order.asc(order)); return findByCriteria(page.getNo(),
	 * page.getSize(), criteria); }
	 */

}
